package com.txzt.gaea.business.ident.controller;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.aspect.annotation.SysDataPermission;
import com.txzt.gaea.business.ident.domain.model.IdentInfo;
import com.txzt.gaea.business.ident.service.IIdentInfoService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.core.base.context.AdminUserContext;
import com.txzt.gaea.common.enums.DeclareStatus;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.vo.AdminUserVO;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ident/info")
public class IdentInfoController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(IdentInfoController.class);

    @Autowired
    private IIdentInfoService identInfoService;

    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @param identInfo
     * @return
     */
    @SysDataPermission
    @GetMapping(value = "/list")
    public RtnInfo<?> pageList(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, IdentInfo identInfo) {
        logger.info(identInfo.toString());
        PageQuery<IdentInfo> pageList = identInfoService.pageQuery(new PageQuery<>(pageNumber, pageSize, identInfo));
        pageList.getList().forEach(item -> {
            //翻译创建人
            item.setCreateName(TranslateUtil.getUserName(item.getCreateBy()));
            //翻译项目组长
            item.setProjectLeaderName(TranslateUtil.getUserName(item.getProjectLeader()));
        });
        return RtnInfo.success(pageList);
    }

    /**
     * 查询待审核数量(鉴定管理)
     * @param identInfo
     * @return
     */
    @SysDataPermission
    @GetMapping(value = "/listCheckCount")
    public RtnInfo<?> listCheckCount(IdentInfo identInfo) {
        Long count = identInfoService.listCheckCount(identInfo);
        return RtnInfo.success(count);
    }

    /**
     * id查询
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public RtnInfo<?> queryById(@RequestParam Long id) {
        IdentInfo identInfo = identInfoService.selectIdentInfoById(id);
        if (null == identInfo) {
            return RtnInfo.SERVER_ERROR;
        }else {
            return RtnInfo.success(identInfo);
        }

    }

    /**
     * 新增
     * @param identInfo
     * @return
     */
    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> add(@RequestBody IdentInfo identInfo) {
        AdminUserVO adminUserVO = AdminUserContext.getUser();
        identInfo.setDeclareStatus(DeclareStatus.INIT.getCode());
        identInfo.setIsDelete(IsDeleteEnum.NOTDELETE.getCode());
        identInfo.setDeptId(adminUserVO.getDeptid());
        int count = identInfoService.insertTemplate(identInfo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 提交
     * @param identInfo
     * @return
     */
    @PutMapping(value = "/submit")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> submit(@RequestBody IdentInfo identInfo) {
        AdminUserVO adminUserVO = AdminUserContext.getUser();
        identInfo.setDeptId(adminUserVO.getDeptid());
        int count = identInfoService.submitIdentInfo(identInfo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 编辑
     * @param identInfo
     * @return
     */
    @PutMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> edit(@RequestBody IdentInfo identInfo) {
        AdminUserVO adminUserVO = AdminUserContext.getUser();
        identInfo.setDeptId(adminUserVO.getDeptid());
        int count = identInfoService.updateById(identInfo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> delete(@RequestParam Long id) {
        IdentInfo identInfo = new IdentInfo();
        identInfo.setId(id);
        identInfo.setIsDelete(IsDeleteEnum.DELETE.getCode());
        int count = identInfoService.updateById(identInfo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 撤回
     * @param identInfo
     * @return
     */
    @PutMapping(value = "/revocation")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> revocation(@RequestBody IdentInfo identInfo) {
        int count = identInfoService.revocationIdent(identInfo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

}

